package com.example.text334;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List<Data> list;
    EditText name, num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = findViewById(R.id.name);
        num = findViewById(R.id.num);
    }
    public void insert(View view){
        String names = name.getText().toString();
        String nums = num.getText().toString();
        if (names.equals("")||nums.equals("")){
            Toast.makeText(MainActivity.this, "请完善您的信息", Toast.LENGTH_LONG).show();
        }else {
        MySQL mySQL = new MySQL(MainActivity.this);
        SQLiteDatabase db = mySQL.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", names);
            values.put("num", nums);
            long person = db.insert("person", null, values);
            System.out.println(person);
            Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_LONG).show();
            db.close();
        }
    }
    public void delete(View view){
        String names = name.getText().toString();
        MySQL mySQL = new MySQL(MainActivity.this);
        SQLiteDatabase db = mySQL.getWritableDatabase();
        int person = db.delete("person", "name=?", new String[]{names});
        if (person>0)
        Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(MainActivity.this, "删除信息不存在", Toast.LENGTH_LONG).show();
        db.close();
    }
    public void change(View view){
        String nums = num.getText().toString();
        String names = name.getText().toString();
        MySQL mySQL = new MySQL(MainActivity.this);
        SQLiteDatabase db = mySQL.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("num", nums);
        int person = db.update("person", values, "name=?", new String[]{names});
        if (person>0)
            Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(MainActivity.this, "修改信息不存在", Toast.LENGTH_LONG).show();
        db.close();
    }
    public void query(View view){
        list = new ArrayList<>();
        String names = name.getText().toString();
        MySQL mySQL = new MySQL(MainActivity.this);
        SQLiteDatabase db = mySQL.getWritableDatabase();
        Cursor c = db.query("person", null, "name=?", new String[]{names}, null, null, null);
        int count = 0;
        while(c.moveToNext()){
          count++;
           list.add(new Data(c.getString(0), c.getString(1)));
        }
        if(count==0)
            Toast.makeText(MainActivity.this, "查询信息不存在", Toast.LENGTH_LONG).show();
        ListView listView;
        listView = findViewById(R.id.listview);
        listView.setAdapter(new Myadaper());
        c.close();
        db.close();
    }
    public void all(View view) {
        list = new ArrayList<>();
        String names = name.getText().toString();
        MySQL mySQL = new MySQL(MainActivity.this);
        SQLiteDatabase db = mySQL.getWritableDatabase();
        Cursor c = db.query("person", null, null, null, null, null, null);
        int count = 0;
        while(c.moveToNext()){
            count++;
            list.add(new Data(c.getString(0), c.getString(1)));
        }
        if(count==0)
            Toast.makeText(MainActivity.this, "信息为空", Toast.LENGTH_LONG).show();
        ListView listView;
        listView = findViewById(R.id.listview);
        listView.setAdapter(new Myadaper());
        c.close();
        db.close();
    }

    class Myadaper extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this, R.layout.activity_item, null);
            TextView t1 = view.findViewById(R.id.textView3);
            TextView t2 = view.findViewById(R.id.textView4);
            t1.setText(list.get(position).getName());
            t2.setText(list.get(position).getNum());
            return view;
        }
    }
}